package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"syscall"
)

func main() {
	//copyFileWithIoCopy()
	countChars()
}

// copyFileWithIO
//
//	@Description: 复制文件 io.Copy(dst Writer, src Reader)
func copyFileWithIoCopy() {
	read := "E:\\goprojects\\src\\helloworld01\\src\\helloword01\\24file\\00_testfile\\001.png"
	write := "E:\\goprojects\\src\\helloworld01\\src\\helloword01\\24file\\00_testfile\\001_copy.png"
	//打开要读取的文件
	readFile, err := os.Open(read)
	if err != nil {
		fmt.Println("read file err=", err)
	}
	defer readFile.Close()
	//打开要写的文件。如果不存在则create
	writeFile, err := os.OpenFile(write, syscall.O_WRONLY|syscall.O_CREAT, 0666)
	if err != nil {
		fmt.Println("write file err=", err)
	}
	defer writeFile.Close()
	//获取reader
	reader := bufio.NewReader(readFile)
	//获取writer
	writer := bufio.NewWriter(writeFile)
	//调用io.Copy()
	written, err := io.Copy(writer, reader)
	if err != nil {
		fmt.Println("copy file err=", err)
	} else {
		fmt.Println(written) //5,907字节
	}
}

// countChars
//
//	@Description: 统计各种字符类型的个数
func countChars() {
	read := "E:\\goprojects\\src\\helloworld01\\src\\helloword01\\24file\\00_testfile\\test01.txt"
	readFile, err := os.Open(read)
	if err != nil {
		fmt.Println("open file err=", err)
		return
	}
	defer readFile.Close()
	reader := bufio.NewReader(readFile)
	var count Count
	for {
		value, err := reader.ReadString('\n')
		if err == io.EOF {
			break
		}
		for _, v := range value {

			switch {
			case v >= 'a' && v <= 'z':
				count.enCount++
			case v >= 'A' && v <= 'Z':
				count.EnCount++
			case v >= '0' && v <= '9':
				count.NumCount++
			case v == ' ' || v == '\t':
				count.SpaceCount++
			default:
				count.OtherCount++
			}
		}
	}
	fmt.Println(count)

}

type Count struct {
	NumCount   int //
	enCount    int
	EnCount    int
	SpaceCount int
	OtherCount int
}
